<!--
  功能：接种记录
  作者：zzb
  邮箱：2283325110@qq.com
  时间：2021年12月06日 17:05:38
  版本：v1.0
  修改记录：
  修改内容：
  修改人员：
  修改时间：
-->
<template>
  <div class="">
    <vab-query-form-left-panel style="margin-top: 20px; margin-left: 20px">
      <el-form ref="form" :model="query" :inline="true" @submit.native.prevent>
        <el-form-item>
          <el-input
            v-model="query.name"
            clearable="true"
            placeholder="姓名"
            style="width: 100%"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="query.start"
            type="date"
            placeholder="起始日期"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <span style="margin-right: 10px">-</span>
        <el-form-item>
          <el-date-picker
            v-model="query.end"
            type="date"
            placeholder="截至日期"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            icon="el-icon-search"
            type="primary"
            native-type="submit"
            @click="handleQuery"
          >
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </vab-query-form-left-panel>
    <el-table
      ref="tableSort"
      :data="tableData"
      :header-cell-style="{ background: '#F3F4F7', color: '#555' }"
      :border="true"
      :row-style="{
        fontSize: '12px',
        color: '#666',
        fontFamily: 'MicrosoftYaHeiUI',
      }"
    >
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="gender" label="性別"></el-table-column>
      <el-table-column prop="age" label="年齡"></el-table-column>
      <el-table-column label="第一针时间">
        <template #default="{ row }">
          <span v-if="row.firstDate != null">{{ row.firstDate }}</span>
          <span v-else><el-tag type="danger">暂未接种</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column label="第二针时间">
        <template #default="{ row }">
          <span v-if="row.secondDate != null">{{ row.secondDate }}</span>
          <span v-else><el-tag type="danger">暂未接种</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="permissions[0] != 'editor'"
        show-overflow-tooltip
        label="操作"
      >
        <template #default="{ row }">
          <!-- <el-button type="warning" @click="handleEdit(row)">修改</el-button> -->
          <el-button type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-empty v-show="tableData == null" description="暂无记录"></el-empty>
    <el-pagination
      :background="background"
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="5"
      :page-sizes="[3, 5, 7, 9]"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
  </div>
</template>

<script>
  import { queryPageHistory, deleteHistory } from '@/api/history'
  import { mapGetters } from 'vuex'
  export default {
    // 组件名称
    name: 'History',
    components: {},
    // 组件参数 接收来自父组件的数据
    props: {},
    // 组件状态值
    data() {
      return {
        query: {},
        tableData: [],
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        queryForm: {
          pageNum: 1,
          pageSize: 5,
        },
      }
    },
    // 计算属性
    computed: {
      ...mapGetters({
        username: 'user/username',
        permissions: 'user/permissions',
      }),
    },
    // 侦听器
    watch: {},
    // 以下是生命周期钩子
    /**
     * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
     */
    created() {
      this.fetchData()
    },
    /**
     * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
     * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
     */
    mounted() {
      this.fetchData()
    },
    // 组件方法
    methods: {
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      async handleQuery() {
        // this.fetchData()
        if (this.query.name || this.query.start || this.query.end) {
          let start = new Date(this.query.start).getTime()
          console.log(start)
          let end = new Date(this.query.end).getTime()
          let name = this.query.name
          let tableData = this.tableData.filter((item) => {
            if (
              (item.name == name || !name) &&
              (start == new Date(item.firstDate).getTime() ||
                start < new Date(item.firstDate).getTime() ||
                !start) &&
              (end == new Date(item.firstDate).getTime() ||
                end > new Date(item.firstDate).getTime() ||
                !end)
            ) {
              return item
            }
          })
          this.tableData = tableData
        } else this.fetchData()
      },
      cancel(userName) {
        deleteHistory(userName).then((res) => {
          this.$baseMessage(res.msg, 'success')
          this.fetchData()
        })
      },
      async fetchData() {
        this.tableData = []
        const { data } = await queryPageHistory(this.queryForm)
        if (data.list == null) return
        if (this.permissions[0] == 'editor') {
          this.tableData = data.list.filter((item) => {
            return item.userName == this.username
          })
        } else this.tableData = data.list
        this.total = data.total
      },
      handleDelete(row) {
        this.$confirm('此操作将删除该记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            this.cancel(row.userName)
            this.fetchData()
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '操作取消',
            })
          })
      },
    },
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style scoped></style>
